package org.csu.canteen.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.canteen.entity.*;
import org.csu.canteen.persistence.*;
import org.csu.canteen.service.ItemService;
import org.csu.canteen.service.OrderService;
import org.csu.canteen.vo.AccountVO;
import org.csu.canteen.vo.CartVO;
import org.csu.canteen.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private SequenceMapper sequenceMapper;

    @Autowired
    private LineItemMapper lineItemMapper;

    @Autowired
    private ItemService itemService;

    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Override
    public void insertOrder(Orders orders) {
        ordersMapper.insert(orders);
        if (orders.getOrderId() == null) {
            orders.setOrderId(getNextId("ordernum"));
        }
        //获取订单中的lineitem
        List<LineItem> lineItemList=orders.getLineItems();

        //更新inventory表
        for (LineItem lineItem : lineItemList) {
            String itemId = lineItem.getItemId();
            Integer increment = lineItem.getQuantity();
            Map<String, Object> param = new HashMap<String, Object>(2);
            param.put("itemId", itemId);
            param.put("increment", increment);
            itemService.updateInventoryQuantity(param);
        }

        //更新lineitem表
        int lineNumber = 1; // 从 1 开始递增
        for (LineItem lineItem : lineItemList) {
            lineItem.setOrderId(orders.getOrderId());
            lineItem.setLineNumber(lineNumber++);
            lineItemMapper.insert(lineItem);
        }

        //更新orderstauts表
        OrderStatus orderStatus=new OrderStatus();
        orderStatus.setOrderId(orders.getOrderId());
        orderStatus.setLineNumber(orders.getOrderId());
        orderStatus.setTimeStamp(orders.getOrderDate());
        orderStatus.setStatus(orders.getStatus());
        orderStatusMapper.insert(orderStatus);
    }



    public Orders getOrderById(int orderId) {
        return ordersMapper.selectById(orderId);
    }


    public List<Orders> getOrdersByUsername(String username) {
        //条件查询
        QueryWrapper<Orders> queryWrapper=new QueryWrapper<>();
        //构造条件
        queryWrapper.eq("userid",username);
        return ordersMapper.selectList(queryWrapper);
    }

    @Override
    public int getNextId(String name) {
        Sequence sequence = sequenceMapper.selectById(name);
        if (sequence == null) {
            throw new RuntimeException("Error: A null sequence was returned from the database (could not get next " + name + " sequence).");
        }
        int nextId = sequence.getNextId() + 1;
        sequenceMapper.updateSequence(name, nextId);
        return nextId;
    }


}
